我对org-download的兴趣被 org-mode
邮件列表中的 这个帖子 在今天重新激发起来了。
org-download
允许你拖拽一个图像从像Firefox或文件系统到一个 org-mode
缓冲区内。
我最初写这篇文章的时候,正在 edX 上学习一门化学课,课上有很多作业和考试,其中包括很多图片。
在使用 org-mode
以文学的方式完成作业时,我希望问题是完全自包含的,为此我需要快速保存网站上的所有图像到 org-mode
缓冲区中。
随着一些hack和资深黑客的建议和贡献, org-download
应运而生。
今天提交的主要贡献是添加了代码取消指向HTML的图像别名。我在Twitter上看到很多人都这样用,@_abo_abo。这是代码:
(defcustom org-download-img-regex-list
'("<img +src=\"" "<img +\\(class=\"[^\"]+\"\\)? *src=\"")
"This regex is used to unalias links that look like images.
The html to which the links points will be searched for these
regexes, one by one, until one succeeds. The found image address
will be used."
:group 'org-download)
并在 (org-download-image link)
中加上:
(unless (image-type-from-file-name link)
(with-current-buffer
(url-retrieve-synchronously link t)
(let ((regexes org-download-img-regex-list)
lnk)
(while (and (not lnk) regexes)
(goto-char (point-min))
(when (re-search-forward (pop regexes) nil t)
(backward-char)
(setq lnk (read (current-buffer)))))
(if lnk
(setq link lnk)
(error "link %s does not point to an image" link)))))
这里, image-type-from-file-name
内置于 image.el
, 它判断 link
是否是图像文件.
正如你所见,因为我打算在 link
转换之后立即使用,所以我这里使用的是 url-retrieve-synchronly
,而不是异步的 url-retrieve
. 这个 url-retrieve
默认用于下载实际的图像。
注意,在遍历列表时使用了 while
/ pop
组合。我在Emacs核心看到过这种使用发那个法,我觉得这非常简洁。
列表中正则表达式的顺序决定了它们的优先级。例如, org-download-img-regex-list
中的第一个元素匹配Twitter上实际引用的图片,而第二个元素在没有引用图片的情况下将匹配个人的资料图片。
我认为我可能对 org-download
的自定义选项做了过多的设计。
为了让你有足够的动力去发现它们,这里有 一个Youtube演示 的连接,演示了定制(键盘的敲击声)和使用(鼠标的点击声)。